<?php

namespace app\admin\controller;

use think\Controller;

class Common extends Controller
{
    public function initialize()
    {
        //判断管理员是否登录
        if (!session('user_data.id', '', 'account') || !session('username', '', 'account')) {
            $this->redirect('passport/index');
        }
    }

    /**
     * @return array
     * 获取当前登录的用户的菜单权限
     */
    public function menu()
    {
        $menus = $this->menuList();

        //权限管理 start
        //当前操作者权限ID
        if (!session('is_super', '', 'account')) {//不是超级管理员，就要筛选该用户能操作的菜单
            //当前管理员权限
            $roles = db('desktop_roles')->where('role_id', session('user_data.rid', '', 'account'))->value('role_list');

            if ($roles) {
                $roles = explode(',', $roles);
                foreach ($menus as $key => $menu) {
                    $children = array();
                    foreach ($menu['children'] as $r_children) {
                        if (in_array($r_children['id'], $roles)) {
                            if (empty($r_children['children'])) unset($r_children['children']);//后台导航菜单只显示2级，大于的都去掉
                            $children[] = $r_children;
                        } else if (reset($roles) == 'all') {//$roles[0] = all 代表用户拥有超级管理员权限
                            if (empty($r_children['children'])) unset($r_children['children']);//后台导航菜单只显示2级，大于的都去掉
                            $children[] = $r_children;
                        }
                    }
                    if (!empty($children)) {
                        $menus[$key]['children'] = $children;
                    } else {
                        unset($menus[$key]);
                    }
                }
                $a = [];
                foreach ($menus as $menu){
                    $a[] = $menu;
                }
                $menus = $a;
            } else {
                $menus = array();
            }
        }
        //权限管理 end

        return $menus;
    }

    /**
     * @return array
     * 后台界面的菜单集合
     * 左边的导航只显示到二级数组
     */
    public function menuList()
    {
        $role = [
            [
                'title' => '系统设置', 'href' => '#', 'icon' => '&#xe614;', 'id' => '1', 'pid' => '0',
                'children' => [
                    ['title' => '系统设置', 'href' => url('System/system'), 'id' => '10', 'pid' => '1', 'icon' => '&#xe716;'],
                    ['title' => '站点配置', 'href' => url('Setting/index'), 'id' => '17', 'pid' => '1', 'icon' => '&#xe674;'],
                    ['title' => '邮箱设置', 'href' => url('System/email'), 'id' => '11', 'pid' => '1', 'icon' => '&#xe631;'],
                    ['title' => '短信设置', 'href' => url('System/sms'), 'id' => '12', 'pid' => '1', 'icon' => '&#xe716;'],
                    ['title' => '短信发送记录', 'href' => url('Service/SmsLog'), 'id' => '19', 'pid' => '1', 'icon' => '&#xe66e;'],
                    ['title' => '合作伙伴', 'href' => url('System/partnerList'), 'id' => '13', 'pid' => '1', 'icon' => '&#xe608;'],
                    ['title' => '招聘信息', 'href' => url('System/recruitment'), 'id' => '15', 'pid' => '1', 'icon' => '&#xe667;'],
                    ['title' => '友情链接', 'href' => url('System/friendLink'), 'id' => '14', 'pid' => '1', 'icon' => '&#xe64c;'],
                    ['title' => 'PC导航菜单', 'href' => url('Menu/index', array('type' => 'pc')), 'id' => '16', 'pid' => '1', 'icon' => '&#xe64c;'],
                    ['title' => 'WAP导航菜单', 'href' => url('Menu/index', array('type' => 'wap')), 'id' => '16', 'pid' => '1', 'icon' => '&#xe64c;'],
                    ['title' => '图片列表', 'href' => url('Image/index'), 'id' => '18', 'pid' => '1', 'icon' => '&#xe64a;'],
                ]
            ],
            [
                'title' => '权限管理', 'href' => '#', 'icon' => '&#xe672;', 'id' => '2', 'pid' => '0',
                'children' => [
                    ['title' => '管理员列表', 'href' => url('User/managerList'), 'id' => '20', 'pid' => '2', 'icon' => "&#xe770;",
                        'children' => []
                    ],
                    ['title' => '角色列表', 'href' => url('Role/index'), 'id' => '21', 'pid' => '2', 'icon' => "&#xe770;"]
                ]
            ],
            [
                'title' => '数据库管理', 'href' => '#', 'icon' => '&#xe770;', 'id' => '3', 'pid' => '0',
                'children' => [
                    ['title' => '数据库备份', 'href' => url('Database/database'), 'id' => '30', 'pid' => '3', 'icon' => "&#xe638;"],
                    ['title' => '还原数据库', 'href' => url('Database/restore'), 'id' => '31', 'pid' => '3', 'icon' => "&#xe638;"]
                ]
            ],
            [
                'title' => '开发者管理', 'href' => '#', 'icon' => '&#xe770;', 'id' => '4', 'pid' => '0',
                'children' => [
                    ['title' => '开发者列表', 'href' => url('Develop/index'), 'id' => '40', 'pid' => '4', 'icon' => "&#xe60a;"],
                    ['title' => '开发者认证', 'href' => url('Authenticate/index'), 'id' => '41', 'pid' => '4', 'icon' => "&#xe672;"],
                    ['title' => '开发者等级', 'href' => url('DevelopLv/index'), 'id' => '42', 'pid' => '4', 'icon' => "&#xe62a;"],
                    ['title' => '提现列表', 'href' => url('Develop/transfer'), 'id' => '43', 'pid' => '4', 'icon' => "&#xe66c;"],
                    ['title' => '在线文档分类', 'href' => url('Develop/articleCat'), 'id' => '44', 'pid' => '4', 'icon' => "&#xe705;"],
                    ['title' => '在线课堂分类', 'href' => url('Develop/olClassCat'), 'id' => '45', 'pid' => '4', 'icon' => "&#xe6ed;"],
                    ['title' => '在线文档列表', 'href' => url('Develop/articlesList'), 'id' => '46', 'pid' => '4', 'icon' => "&#xe705;"],
                    ['title' => '在线课堂列表', 'href' => url('Develop/olClassList'), 'id' => '47', 'pid' => '4', 'icon' => "&#xe6ed;"],
                    ['title' => '在线课堂视频总表', 'href' => url('Develop/classVideoList'), 'id' => '48', 'pid' => '4', 'icon' => "&#xe6ed;"],
                ]
            ],
            [
                'title' => '会员管理', 'href' => '#', 'icon' => '&#xe66f;', 'id' => '5', 'pid' => '0',
                'children' => [
                    ['title' => '会员列表', 'href' => url('Members/index'), 'id' => '50', 'pid' => '5', 'icon' => "&#xe66f;"],
                    ['title' => '等级列表', 'href' => url('Members/lvIndex'), 'id' => '51', 'pid' => '5', 'icon' => "&#xe735;"],
                    ['title' => '充值交易', 'href' => url('Members/advanceIndex'), 'id' => '52', 'pid' => '5', 'icon' => "&#xe66e;"],
                ]
            ],
            [
                'title' => '应用管理', 'href' => '#', 'icon' => '&#xe653;', 'id' => '6', 'pid' => '0',
                'children' => [
                    ['title' => '应用列表', 'href' => url('Goods/index', array('type' => 'app')), 'id' => '60', 'pid' => '6', 'icon' => "&#xe653;"],
                    ['title' => '服务列表', 'href' => url('Goods/index', array('type' => 'server')), 'id' => '61', 'pid' => '6', 'icon' => "&#xe632;"],
                    ['title' => '模板列表', 'href' => url('Goods/index', array('type' => 'template')), 'id' => '62', 'pid' => '6', 'icon' => "&#xe656;"],
                    ['title' => '升级审核', 'href' => url('Goods/upgrade'), 'id' => '63', 'pid' => '6', 'icon' => "&#xe604;"],
                    ['title' => '规格列表', 'href' => url('AppSpec/index'), 'id' => '64', 'pid' => '6', 'icon' => "&#xe60a;"],
                    ['title' => '类型列表', 'href' => url('AppType/index'), 'id' => '65', 'pid' => '6', 'icon' => "&#xe60a;"],
                    ['title' => '分类列表', 'href' => url('AppCat/index'), 'id' => '66', 'pid' => '6', 'icon' => "&#xe60a;"],
                    ['title' => '模块列表', 'href' => url('AppModule/index'), 'id' => '67', 'pid' => '6', 'icon' => "&#xe60a;"],
                    ['title' => '支持列表', 'href' => url('AppSupport/index'), 'id' => '68', 'pid' => '6', 'icon' => "&#xe60a;"],
                ]
            ],
            [
                'title' => '订单管理', 'href' => '#', 'icon' => '&#xe655;', 'id' => '7', 'pid' => '0',
                'children' => [
                    ['title' => '订单列表', 'href' => url('Orders/index'), 'id' => '70', 'pid' => '7', 'icon' => "&#xe667;"],
                    ['title' => '客户端订单', 'href' => url('Orders/client'), 'id' => '74', 'pid' => '7', 'icon' => "&#xe667;"],
                    ['title' => '退款列表', 'href' => url('Orders/aftersale'), 'id' => '71', 'pid' => '7', 'icon' => "&#xe667;"],
                    ['title' => '订单评论', 'href' => url('OrderComment/index'), 'id' => '72', 'pid' => '7', 'icon' => "&#xe611;"],
                    ['title' => '订单日志', 'href' => url('OrdersLog/index'), 'id' => '73', 'pid' => '7', 'icon' => "&#xe66e;"],
                ]
            ],
            [
                'title' => '插件管理', 'href' => '#', 'icon' => '&#xe631;', 'id' => '8', 'pid' => '0',
                'children' => [
                    ['title' => '支付设置', 'href' => url('Plugins/payment'), 'id' => '80', 'pid' => '8', 'icon' => "&#xe6b1;"],
                    ['title' => '登录设置', 'href' => url('Plugins/login'), 'id' => '81', 'pid' => '8', 'icon' => "&#xe6b1;"],
                ]
            ],
            [
                'title' => '客户端管理', 'href' => '#', 'icon' => '&#xe756;', 'id' => '9', 'pid' => '0',
                'children' => [
                    ['title' => '客户端设置', 'href' => url('SiteClient/index'), 'id' => '90', 'pid' => '9', 'icon' => "&#xe756;"],
                    ['title' => '下载日志', 'href' => url('SiteLog/index'), 'id' => '91', 'pid' => '9', 'icon' => "&#xe66e;"],
                ]
            ],
            [
                'title' => '服务相关', 'href' => '#', 'icon' => '&#xe735;', 'id' => '100', 'pid' => '0',
                'children' => [
                    ['title' => '服务板块', 'href' => url('Service/serviceinfocat'), 'id' => '105', 'pid' => '100', 'icon' => "&#xe60b;"],
                    ['title' => '服务信息', 'href' => url('Service/serviceinfos'), 'id' => '106', 'pid' => '100', 'icon' => "&#xe60b;"],
                    ['title' => '工单类型', 'href' => url('Service/questioncat'), 'id' => '101', 'pid' => '100', 'icon' => "&#xe60b;"],
                    ['title' => '工单记录', 'href' => url('Service/questions'), 'id' => '102', 'pid' => '100', 'icon' => "&#xe60b;"],
                    ['title' => '需求类型', 'href' => url('Service/demandcat'), 'id' => '103', 'pid' => '100', 'icon' => "&#xe60b;"],
                    ['title' => '需求记录', 'href' => url('Service/demands'), 'id' => '104', 'pid' => '100', 'icon' => "&#xe60b;"],
                    ['title' => '官方公告', 'href' => url('Service/announcements'), 'id' => '107', 'pid' => '100', 'icon' => "&#xe645;"]
                ]
            ],
            [
                'title' => '设备管理', 'href' => '#', 'icon' => '&#xe672;', 'id' => '1100', 'pid' => '0',
                'children' => [
                    ['title' => '设备列表', 'href' => url('Machine/machineList'), 'id' => '1101', 'pid' => '1100', 'icon' => "&#xe670;"],
                    ['title' => '物联网卡列表', 'href' => url('Machine/simList'), 'id' => '1102', 'pid' => '1100', 'icon' => "&#xe670;"],
                    ['title' => '物联网卡收费管理', 'href' => url('Machine/rechangeSetting'), 'id' => '1103', 'pid' => '1100', 'icon' => "&#xe670;"],
                    ['title' => '物联网卡充值日志', 'href' => url('Machine/rechangeLog'), 'id' => '1104', 'pid' => '1100', 'icon' => "&#xe670;"],
                ]
            ],
            [
                'title' => '站点管理', 'href' => '#', 'icon' => '&#xe7ae;', 'id' => '11', 'pid' => '0',
                'children' => [
                    ['title' => '站点列表', 'href' => url('WebSite/index'), 'id' => '111', 'pid' => '11', 'icon' => "&#xe638;"],
                    ['title' => '站点评论', 'href' => url('WebSite/discuss'), 'id' => '112', 'pid' => '11', 'icon' => "&#xe611;"],
                ]
            ],

            [
                'title' => '版本更新', 'href' => '#', 'icon' => '&#xe665;', 'id' => '12', 'pid' => '0',
                'children' => [
                    ['title' => '版本列表', 'href' => url('WebUpdate/index'), 'id' => '120', 'pid' => '12', 'icon' => "&#xe638;"],
                    ['title' => '添加版本', 'href' => url('WebUpdate/add'), 'id' => '121', 'pid' => '12', 'icon' => "&#xe638;"]
                ]
            ],
//            [
//                'title' => '盗版追踪', 'href' => '#', 'icon' => 'icon-spinner', 'id' => '100', 'pid' => '0',
//                'children' => [
//                    ['title' => '盗版列表', 'href' => url('WebPirate/index'), 'id' => '105', 'pid' => '100', 'icon' => "&#xe638;"],
//                ]
//            ],
            [
                'title' => '销售报表', 'href' => '#', 'icon' => '&#xe857;', 'id' => '10', 'pid' => '0',
                'children' => [
                    ['title' => '销售统计', 'href' => url('Analysis/sale'), 'id' => '105', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '客户端统计', 'href' => url('Analysis/client'), 'id' => '109', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '余额统计', 'href' => url('Analysis/advance'), 'id' => '105', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '开发者统计', 'href' => url('Analysis/develop'), 'id' => '105', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '物联卡统计', 'href' => url('Analysis/card'), 'id' => '105', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '退款统计', 'href' => url('Analysis/refund'), 'id' => '106', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '提现统计', 'href' => url('Analysis/transfer'), 'id' => '107', 'pid' => '10', 'icon' => "&#xe62c;"],
                    ['title' => '经营统计', 'href' => url('Analysis/index'), 'id' => '108', 'pid' => '10', 'icon' => "&#xe62c;"],
                ]
            ],
            [
                'title' => '新闻资讯', 'href' => '#', 'icon' => '&#xe705;', 'id' => '13', 'pid' => '0',
                'children' => [
                    ['title' => '资讯横幅', 'href' => url('Information/banners'), 'id' => '130', 'pid' => '13', 'icon' => "&#xe645;"],
                    ['title' => '资讯类目', 'href' => url('Information/index'), 'id' => '131', 'pid' => '13', 'icon' => "&#xe638;"],
                    ['title' => '资讯列表', 'href' => url('Information/article'), 'id' => '132', 'pid' => '13', 'icon' => "&#xe609;"],
                ]
            ],
            [
                'title' => '熊掌号管理', 'href' => '#', 'icon' => '&#xe628;', 'id' => '14', 'pid' => '0',
                'children' => [
                    ['title' => '熊掌号设置', 'href' => url('System/baidu'), 'id' => '140', 'pid' => '14', 'icon' => '&#xe716;'],
                    ['title' => '新增内容提交', 'href' => url('Baidu/newContent'), 'id' => '141', 'pid' => '14', 'icon' => '&#xe642;'],
                    ['title' => '历史内容提交', 'href' => url('Baidu/historyContent'), 'id' => '142', 'pid' => '14', 'icon' => '&#xe642;'],
                ]
            ],
        ];
        return $role;
    }

    /**
     * @param $perm_id
     * @return bool
     * 检测是否有权限使用
     */
    function has_permission($perm_id)
    {
        $menus = $this->menuList();

        $target = '';
        if(is_numeric($perm_id)){

        }else{
            foreach ($menus as $menu){
                if(!empty($menu['children'])){
                    foreach ($menu['children'] as $child){
                        if(strpos($child['href'], $perm_id)){
                            $target = $child['id'];
                        }
                    }
                }
            }
        }
        //当前操作者权限ID
        if (!session('is_super', '', 'account')) {//不是超级管理员，就要筛选该用户能操作的菜单
            //当前管理员权限
            $roles = db('desktop_roles')->where('role_id', session('user_data.rid', '', 'account'))->value('role_list');

            if ($roles && $target) {
                $roles = explode(',', $roles);
                if(in_array($target, $roles)){
                    return true;
                }
            } else {
                return false;
            }
        }else{
            return true;
        }
    }

    //空操作
    public function _empty()
    {
        return $this->error('空操作，返回上次访问页面中...');
    }
}
